/*******************************************************************************
 * Copyright (c) 2011-11-10 @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> - initial API and implementation
 ******************************************************************************/
package com.redhat.auth.ejb.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import org.springframework.stereotype.Service;

import com.dayatang.domain.InstanceFactory;
import com.dayatang.querychannel.service.QueryChannelService;
import com.dayatang.querychannel.support.Page;
import com.redhat.auth.domain.FunctionEntityType;
import com.redhat.auth.ejb.FunctionEntityTypeApplication;
import com.redhat.auth.ejb.vo.FunctionEntityTypeVO;

/**
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
 * @since 2011-11-10
 */
@Service("ramsFunctionEntityTypeApplication")
@Stateless(name = "FunctionEntityTypeApplicationBean")
@Remote(FunctionEntityTypeApplication.class)
@javax.interceptor.Interceptors(SpringEJBIntercepter.class)
public class FunctionEntityTypeApplicationImpl implements
		FunctionEntityTypeApplication {
	private static QueryChannelService queryChannel;

	public static QueryChannelService getQueryChannel() {
		if (queryChannel == null) {
			queryChannel = InstanceFactory
					.getInstance(QueryChannelService.class);
		}
		return queryChannel;
	}

	public static void setQueryChannel(QueryChannelService queryChannel) {
		FunctionEntityTypeApplicationImpl.queryChannel = queryChannel;
	}

	/**
	 * 根据Id返回一个FunctionEntityType对象
	 */
	public FunctionEntityTypeVO get(long id) {
		return Convertor.po2vo(FunctionEntityType.get(id), null);
	}

	/**
	 * 保存一个对象
	 */
	public FunctionEntityTypeVO save(FunctionEntityTypeVO vo) {
		FunctionEntityType type = Convertor.vo2po(vo);
		if (type != null) {
			type.save();
		}
		return Convertor.po2vo(type, null);
	}

	public void delete(long id) {
		FunctionEntityType entityType = FunctionEntityType.get(id);
		if (entityType != null) {
			entityType.remove();
		}
	}

	public List<FunctionEntityTypeVO> findRoot() {
		return (List<FunctionEntityTypeVO>) Convertor.po2vo(FunctionEntityType
				.findRoot(), new ArrayList(), null);
	}

	public List<FunctionEntityTypeVO> queryFunctionEntityType(
			FunctionEntityTypeVO vo) {
		String hql = "from FunctionEntityType t where 1=1 ";
		String name = vo != null ? vo.getName() : null;
		Long parentId = vo != null && vo.getParent() != null
				&& vo.getParent().getId() != null ? vo.getParent().getId()
				: null;
		List<Object> params = new ArrayList<Object>();
		if (name != null && (name = name.trim()).length() > 0) {
			hql += " and t.name like ? ";
			params.add("%" + name + "%");
		}
		if (parentId != null) {
			hql += " and t.parent.id=? ";
			params.add(vo.getParent().getId());
		}
		{
			hql += " order by t.name ";
		}
		List<FunctionEntityType> list = getQueryChannel().queryResult(hql,
				params.toArray());
		List<FunctionEntityTypeVO> result = new ArrayList<FunctionEntityTypeVO>();
		for (FunctionEntityType type : list) {
			result.add(Convertor.po2vo(type, null));
		}
		return result;
	}

	public Page<FunctionEntityTypeVO> pageQueryFunctionEntityType(
			FunctionEntityTypeVO vo, int currentPage, int pageSize) {
		String hql = "from FunctionEntityType t where 1=1 ";
		String name = vo != null ? vo.getName() : null;
		List<Object> params = new ArrayList<Object>();
		if (name != null && (name = name.trim()).length() > 0) {
			hql += " and t.name like ? ";
			params.add("%" + name + "%");
		}
		{
			hql += " order by t.name ";
		}
		Page<FunctionEntityType> page = getQueryChannel()
				.queryPagedResultByPageNo(hql, params.toArray(), currentPage,
						pageSize);
		List<FunctionEntityType> list = page.getResult();
		List<FunctionEntityTypeVO> result = new ArrayList<FunctionEntityTypeVO>();
		for (FunctionEntityType type : list) {
			result.add(Convertor.po2vo(type, null));
		}
		return PageUtil.toPage(page, result);
	}

}
